package com.demo.rocketmq.producer.ha;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class Cluster {

    @Resource(name = "rocketMQTemplate")
    private RocketMQTemplate rocketMqTemplate;

    /**
     * RocketMQ集群部署，有两台master，在发送消息时，其中一个broker宕机，后续发送的消息都会往未宕机的broker发送
     */
    public void send() {
        for (int i = 0; i < 10000; i++) {
            try {
                rocketMqTemplate.syncSendOrderly("test-topic-9", String.format("第%d消息, 日期：%s", i, new Date()), i + "");
            } catch (Exception e) {
                log.error("broker异常", e);
            }
        }
        log.info("发送完毕");
    }
}
